<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
 */

namespace App\Controller\Api;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Request;
use Hyperf\View\RenderInterface;
use Illuminate\Support\Facades\Hash;
class DriverorderController
{


    /*
     * get null
     *
     *
     * return json
     * */

     public function trade_hall(Request $request){
        $user_id = $_SESSION['driver_id'];
        $info = DB::table('order')
            ->select('id','pay_address','get_address','category','product_num','pack_method')
            ->where('status',1)
            ->where('preget',0)
            ->where('driver_status',0)
            ->where('code',1)
            ->where('position',$request->position)
            ->get();
		$info = json_decode($info);
        $res['status'] = true;
        $res['code'] = 200;
  	    $res['msg'] = '';
  	    $res['data'] =$info;
		return json_encode($res,1);
     }

     /*
      * get null
      *
      * return json
      * */


     public function driver_order(){
		$user_id = $_SESSION['driver_id'];
        $info = DB::table('order')
            ->select('id','driver_id','driver_status','order_code','driver_name','product_id','brand',
                'category','standards','product_num','servicer_name','servicer_phone','take_name','take_phone','get_position','get_address',
                'pay_address','supplyer_company','company_name','get_method'
            )
            ->where('driver_id',$user_id)
            ->where('preget',1)
            ->where('traffic_method',0)
            ->get();
		$info = json_decode($info);
        $res['status'] = true;
        $res['code'] = 200;
  	    $res['msg'] = '';
  	    $res['data'] =$info;
		return json_encode($res,1);

     }

     /*
      * get order_id   user_id
      * return json
      * */


     public function order_detail(Request $request){
             $user_id = $_SESSION['driver_id'];
             $info = DB::table('order')
                 ->select('id','driver_id','driver_status','order_code','driver_name','order_product_id','product_id','brand',
                'category','standards','product_num','servicer_name','servicer_phone','take_name','take_phone','get_position','get_address',
                'make_date','pay_address','supplyer_company','company_name','end_time','car_team','car_info','car_num','remarks','get_method'
                 )
                 ->where('id',$request->order_id)
                 ->where('driver_id',$user_id)
                 ->first();

             $product_id = explode(',',$info->order_product_id);
             $order_product= DB::table('order_product')
                 ->select('product_name','standards','brand','category','product_num','pack_method')
                 ->whereIn('id',$product_id)
                 ->get();

            $info = (array)$info;
            $res['status'] = true;
            $res['code'] = 200;
            $res['msg'] = '';
            $res['data']['product'] = $order_product;
            $res['data']['info'] =$info;
            return json_encode($res,1);

     }
     /*
      *
      *
      *
      * */

     public function remarks(Request $request){

             //获取要操作的具体单子
             if(!empty($request->car_team)&&!empty($request->order_id)&&!empty($request->remarks)){
               //$pic = $this->image("remarks_pic",$request);
                $pic = '';
                $remarks_pic = [];
                $remarks_pic[] = $request->remarks_pic;
                foreach ($remarks_pic as $key=>$value){
                     $filename  = $this->image($value);
                     if(!empty($filename)) {
                         $pic_id = DB::table('remarks_pic')->insertGetId(array('name'=>$filename));
                         $pic .= $pic_id.',';
                     }

                }

               $order = DB::table('order')
                   ->select('remarks_pic')
                   ->where('id',$request->order_id)
                   ->where('driver_id',$_SESSION['driver_id'])->first();
               $info['remarks'] = $request->remarks;
               $info['remarks_pic'] = $pic.$order->remarks_pic;
               $info['car_team'] = $request->car_team;
               $info['driver_status'] = 2;
               $info['arriver_date'] = date('Y-m-d H:i:s',time());
               $bool = DB::table('order')
                   ->where('id',$request->order_id)
                   ->where('driver_id',$_SESSION['driver_id'])
                   ->update($info);
               $remark['order_id'] = $request->order_id;
               $remark['remarks'] = $request->remarks;
               $remark['arriver_date'] = $info['arriver_date'];
               $remark['remarks_pic'] = $info['remarks_pic'];
               $remark['end_address'] = $request->end_address;
               $remark['driver_id'] = $_SESSION['driver_id'];
               $remark['driver_name'] = $_SESSION['driver_name'];
               $remark['car_team'] = $request->car_team;

               $driver_remarks = DB::table('driver_remarks')->where('order_id',$request->order_id)->first();
               if(empty($driver_remarks)) {
                   DB::table('driver_remarks')->insert($remark);
               }else{
                   DB::table('driver_remarks')->where('order_id',$request->order_id)->update($remark);
               }

              if($bool){
                    $res['status'] = true;
                    $res['code'] = 200;
                    $res['msg'] = '上传成功';
                    $res['data']  = (object)array();
                    return json_encode($res,1);
              }else{
                    $res['status'] = false;
                    $res['code'] = 400;
                    $res['msg'] = '上传失败';
                    $res['data']  = array();
                    return json_encode($res,1);
              }

             }else{
                    $res['status'] = false;
                    $res['code'] = 400;
                    $res['msg'] = '上传失败';
                    $res['data']  = array();
                    return json_encode($res,1);
             }
     }



     /*
      *
      * */

     public function recommend(Request $request){

         $info = DB::table('order')
             ->select('id','pay_address','get_address')
             ->where('status',1)
             ->where('code',1)
             ->where('preget',0)
             ->where('position',$request->position)
             ->first();
            $info = (array)$info;
            $res['status'] = true;
            $res['code'] = 200;
            $res['msg'] = '';
            $res['data'] =$info;
            return json_encode($res,1);
    }



    /*
     * 公司任务分配
     * */
    public function company_send(){
           $order_to_driver = DB::table('order_to_driver')
               ->select('order_id')
               ->where('driver_id',$_SESSION['driver_id'])
               ->first();
            if(!empty($order_to_driver)) {
                $res['status'] = true;
                $res['code'] = 200;
                $res['msg'] = '公司分配你任务';
                $res['data'] = (array)$order_to_driver;
                return json_encode($res, 1);
            }else{
                $res['status'] = false;
                $res['code'] = 400;
                $res['msg'] = '公司没有分配你任务';
                $res['data'] = [];
                return json_encode($res, 1);
            }
    }



    /*
     * 拒绝公司任务分配
     * */
    public function company_reject(){

          $order_to_driver = DB::table('order_to_driver')
               ->where('driver_id',$_SESSION['driver_id'])
               ->delete();

          DB::table('driver')->where('id',$_SESSION['driver_id'])->update(array('recommend_status'=>0));

            $res['status'] = true;
            $res['code'] = 200;
            $res['msg'] = '';
            $res['data'] =[];
            return json_encode($res,1);

    }







    /*
     * 司机提到货
     * */

    public function get_product(Request $request){

            $order_id = $request->order_id;
            $order = DB::table('order')
                ->select('driver_status')
                ->where('id',$order_id)->first();
            if($order->driver_status == 0){
                DB::table('order')->where('id',$order_id)->update(array('driver_status'=>1,'driver_get_date'=>date('Y-m-d H:i:s',time())));
                $res['status'] = true;
                $res['code'] = 200;
                $res['msg'] = '';
                $res['data'] =[];
            }else{
                $res['status'] = false;
                $res['code'] = 400;
                $res['msg'] = '';
                $res['data'] =[];
            }
            return json_encode($res,1);
    }

    /*
     * 抢购订单
     *user_id
     *order_id
     *
     *
     * */

    public function rush(Request $request){
            $user_id = $_SESSION['driver_id'];

            if(isset($request->order_id)){
               $data = DB::table('order')
                   ->where('status',1)
                   ->where('preget',0)
                   ->where('code',1)
                   ->where('id',$request->order_id)
                   ->first();
               if(!empty($data)){
                   $bool = DB::table('order')
                       ->where('id',$request->order_id)
                       ->update(array('driver_id'=>$user_id,'preget'=>1,'driver_name'=>$_SESSION['driver_name'],
                           'driver_get_date'=>date('Y-m-d H:i:s',time())
                       ));
                   DB::table('driver')
                       ->where('id',$_SESSION['driver_id'])
                       ->update(array('recommend_status'=>0,'work_status'=>1));
                   DB::table('order_to_driver')->where('driver_id',$_SESSION['driver_id'])->delete();
               }else{
                    $res['status'] = false;
                    $res['code'] = 400;
                    $res['msg'] = '接单失败';
                    $res['data']  =array();
                    return json_encode($res,1);
               }

            }else{
                    $res['status'] = false;
                    $res['code'] = 400;
                    $res['msg'] = '接单失败';
                    $res['data']  =array();
                    return json_encode($res,1);
            }

            if($bool){
                    $res['status'] = true;
                    $res['code'] = 200;
                    $res['msg'] = '接单成功';
                    $res['data']  =array();
                    return json_encode($res,1);
            }else{
                    $res['status'] = true;
                    $res['code'] = 400;
                    $res['msg'] = '接单失败';
                    $res['data']  =array();
                    return json_encode($res,1);
            }
    }

      /*
       *
       *
       *
       *
       *
       * */
    //return string
    public function image($file){

            if($file->getmimeType() == "image/png" || $file->getmimeType() == "image/jpeg"){
                $extension = $file->extension();//图片路径
                $name = date('YmdHis',time()).rand(011200000,99999999999).'.'.$extension;
                $file->move("./remarks",$name);//移动至框架图片文件夹
                return $name;
            }
    }












}


?>